<template>
  <van-icon
    :color="value === 1 ? 'red' : ''"
    :name="value === 1?'good-job':'good-job-o'"
    @click="onLike"
  />
</template>

<script>
import { like, noLike } from "@/api/article.js";
import { debounce } from "@/utils/index.js";
export default {
  name: "LikeArticle",
  data() {
    return {};
  },

  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },

  created() {},

  methods: {
    onLike: debounce(async function() {
      // 开始请求
      try {
        let status = -1;
        // 如果已经点赞，则取消点赞
        if (this.value === 1) {
          await noLike(this.articleId);
          this.$toast.success("取消点赞");
        } else {
          // 否则添加点赞
          await like(this.articleId);
          this.$toast.success("点赞成功");
          status = 1;
        }
        this.$emit("input", status);
      } catch (err) {
        console.log(err);
        this.$toast.fail("操作失败");
      }
      // 请求结束
    }, 500)
  }
};
</script>

<style scoped lang='less'>
</style>